package cn.agiledata.bank.myspace.service;

import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.PageBean;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.PeerInnerAccount;
import cn.agiledata.bank.myspace.bean.PeerInnerAccountBean;
import cn.agiledata.bank.myspace.bean.PeerInnerAccountQueryBean;
import cn.agiledata.bank.myspace.dao.PeerInnerAccountDao;
import cn.agiledata.bank.myspace.dao.PeerInnerAccountPageList;
import cn.agiledata.bank.myspace.exception.MySpaceException;

public class PeerInnerAccountServiceImpl implements PeerInnerAccountService {
	private PeerInnerAccountDao peerInnerAccountDao;
	private AccountTemplateInter accountTemplate;
	
	
	public void setPeerInnerAccountDao(PeerInnerAccountDao peerInnerAccountDao) {
		this.peerInnerAccountDao = peerInnerAccountDao;
	}


	public void setAccountTemplate(AccountTemplateInter accountTemplate) {
		this.accountTemplate = accountTemplate;
	}


	/**
	 * 得到关联账户列表
	 * @param bean
	 * @return
	 */
	public PageBean getPeerInnerAccountList(PeerInnerAccountQueryBean bean) {
		PeerInnerAccountPageList pageList = new PeerInnerAccountPageList(bean);
		return pageList.getAccountListPageBean();
	}
	
	/**
	 * 根据联系人id得到联系人
	 * @param id
	 * @return
	 */
	public PeerInnerAccount getPeerInnerAccount(long id) {
		return peerInnerAccountDao.getPeerInnerAccount(id);
	}
	/**
	 * 验证Email或者Mobile
	 * @return 如果验证通过返回客户名称,不通过返回错误代码
	 */
	public String validateEmailOrMobile(String emailOrMobile,String accountId) throws MySpaceException {
		Account account = accountTemplate.getAccount(accountId); 
		Account peerAccount = accountTemplate.getAccountByName(emailOrMobile);
		if(StringUtil.isStrEmpty(emailOrMobile)) {
			throw MySpaceException.PEER_INNER_ACCOUNT_MUST_INPUT;
		}
		if(peerAccount == null) {	//不存在此账户
			throw MySpaceException.NOT_EXISTED_ACCOUNT;
		}
		if(peerAccount.getAccountType().equals(Constant.ACCOUNT_TYPE_INITIAL)) {	//是内部账户
			throw MySpaceException.NOT_EXISTED_ACCOUNT;
		}
		if(!validateNormalEmailOrMobileStatus(peerAccount,emailOrMobile)) {	//Email/手机的状态为有效
			throw MySpaceException.NOT_NORMAL_EMAIL_OR_MOBILE_STATUS;
		}
		if(!validateNotLockAccount(peerAccount)) {	//未冻结的账户
			throw MySpaceException.LOCK_ACCOUNT;
		}
		if(!validateValidCorpCustomer(peerAccount)) {	//企业客户审核通过的账户
			throw MySpaceException.NOT_VALID_CORP_CUSTOMER;
		}
		if(!validateNotSelfAccount(account,peerAccount)) {	//不能添加自己
			throw MySpaceException.CAN_NOT_ADD_MY_ACCOUNT;
		}
		if(!validateUniquePeerInnerAccount(emailOrMobile,accountId)) {	//不允许添加重复的联系人
			throw MySpaceException.NOT_UNIQUE_PEER_INNER_ACCOUNT;
		}
		return StringUtil.getNotNullValue(accountTemplate.getAccountByName(emailOrMobile).getName());
	}
	
	/**
	 * 保存关联账户
	 * @param p
	 */
	public void peerInnerAccountAdd(PeerInnerAccountBean bean) throws MySpaceException {
		validateEmailOrMobile(bean.getEmailOrMobile(),String.valueOf(bean.getAccountId()));
		PeerInnerAccount p = new PeerInnerAccount();
		p = parsePeerInnerAccount(p,bean);
		peerInnerAccountDao.saveOrUpdatePeerInnerAccount(p);
	}
	/**
	 * 修改关联账户
	 * @param id
	 * @throws MySpaceException
	 */
	public void peerInnerAccountModify(PeerInnerAccountBean bean)  throws MySpaceException {
		PeerInnerAccount p = peerInnerAccountDao.getPeerInnerAccount(Long.parseLong(bean.getModifyId()));
		p.setRemark(bean.getRemark());
		peerInnerAccountDao.saveOrUpdatePeerInnerAccount(p);
	}
	
	/**
	 * 删除关联账户 
	 * @param p
	 */
	public void peerInnerAccountDelete(String id) {
		PeerInnerAccount p = peerInnerAccountDao.getPeerInnerAccount(Long.parseLong((id)));
		peerInnerAccountDao.deletePeerInnerAccount(p);
	}
	/**
	 * 验证输入的手机/Email是否是激活的
	 * @param peerAccount
	 * @param emailOrMobile
	 * @return
	 */
	public boolean validateNormalEmailOrMobileStatus(Account peerAccount,String emailOrMobile) {
		if (StringUtil.isEmail(emailOrMobile)) {
			if (peerAccount.getEmail()==null) {
				return false;
			} else if (peerAccount.getEmail().equals(emailOrMobile) && peerAccount.getEmStatus().equals(Constant.ACCOUNT_STATE_NORMAL)) {
				return true;
			} else {
				return false;
			}
		}
		if (StringUtil.isMobile(emailOrMobile)) {
			if (peerAccount.getMobile()==null) {
				return false;
			} else if (peerAccount.getMobile().equals(emailOrMobile) && peerAccount.getMobStatus().equals(Constant.ACCOUNT_STATE_NORMAL)) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}
	/**
	 * 验证未冻结的账户
	 * @param peerAccount
	 * @return
	 */
	public boolean validateNotLockAccount(Account peerAccount) {
		if(peerAccount.getStatus().equals(Constant.ACCOUNT_STATE_LOCK)) {
			return false;
		} else {
			return true;
		}
	}
	/**
	 * 验证企业客户审核通过
	 * @param peerAccount
	 * @return
	 */
	public boolean validateValidCorpCustomer(Account peerAccount) {
		if(peerAccount.getStatus().equals(Constant.ACCOUNT_STATE_PRE_ENTERPRISE)) {
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * 验证是否是自己的账户
	 * @param emailOrMobile
	 * @param myAccountId
	 * @return
	 */
	public boolean validateNotSelfAccount(Account account,Account peerAccount) {
		if(account == null || peerAccount == null) {
			return false;
		} 
		
		if(account.getId().longValue()==peerAccount.getId().longValue()) {
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * 验证是否存在重复的联系人
	 * @param emailOrMobile
	 * @param myAccountId
	 * @return
	 */
	public boolean validateUniquePeerInnerAccount(String emailOrMobile,String myAccountId) {
		PeerInnerAccount peerInnerAccount = peerInnerAccountDao.getPeerInnerAccount(new Long(myAccountId), emailOrMobile);
		if(peerInnerAccount == null) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 构造关联账户对象
	 * @param p
	 * @param form
	 * @return
	 */
	public PeerInnerAccount parsePeerInnerAccount(PeerInnerAccount p,PeerInnerAccountBean bean) {
		p.setAccount(accountTemplate.getAccount(String.valueOf(bean.getAccountId())));	//自己的账户
		p.setPeerAccount(accountTemplate.getAccountByName(bean.getEmailOrMobile()));	//对方账户
		p.setSource(Constant.PEER_ACCOUNT_SOURCE_PREPARATIVE);							//账户来源：联系人
		p.setRemark(bean.getRemark());													//备注
		WeatherUtil.fillCreateAndUpdateInfo(p,String.valueOf(bean.getAccountId()));
		return p;
	}

}
